package com.business.controller;

import com.business.util.FormatUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * Created by mengdexuan on 2017/5/20 12:02.
 */
@Controller
public class ActuatorController {

	private static org.slf4j.Logger log= LoggerFactory.getLogger(ActuatorController.class);

	private RestTemplate restTemplate = new RestTemplate();

	@SuppressWarnings("SpringJavaAutowiringInspection")
	@Autowired
	ManagementServerProperties managementServerProperties;

	@RequestMapping("/actuator")
	public String toActuator() throws Exception {

		log.info("跳转至 Actuator 监控页面……");

		return "actuator";
	}


	@RequestMapping("/actuatorMsg/{param}")
	@ResponseBody
	public String actuatorMsg(@PathVariable String param) throws Exception {
		log.info("actuator 参数 --> "+param);
		String result = "";
		try {
			Integer port = managementServerProperties.getPort();
			String contentPath = managementServerProperties.getContextPath();
			String url = "http://localhost:"+port+"/"+contentPath+"/"+param;
			if ("shutdown".equals(param)){
				ResponseEntity<String> temp = restTemplate.postForEntity(url, null, String.class);
				result = temp.getBody();
			}else {
				result = restTemplate.getForObject(url,String.class);
			}

			result = FormatUtil.formatJson2(result);
		}catch (Exception e){
			e.printStackTrace();
			result = e.getMessage();
		}

		log.info(result);

		return result;
	}


}
